package com.shinhye.elephantsafe;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.shinhye.elephantsafe.service.ShowAddressService;
import com.shinhye.elephantsafe.service.TelephonySafeService;
import com.shinhye.elephantsafe.service.WatchDogService;
import com.shinhye.elephantsafe.ui.SetPreferRelativeLayout;
import com.shinhye.elephantsafe.ui.SetStatusRelativeLayout;
import com.shinhye.elephantsafe.utils.CreatePSWUtils;
import com.shinhye.elephantsafe.utils.CreatePSWUtils.PswBtn;
import com.shinhye.elephantsafe.utils.SystemInfoUtils;
import com.shinhye.elephantsafe.utils.ShortcutUtils;

public class SettingActivity extends Activity {
	private final String TAG = "SettingActivity";
	private SharedPreferences sp;
	private SetStatusRelativeLayout ssrl_autoupdate;
	private SetStatusRelativeLayout ssrl_showaddress;
	private SetPreferRelativeLayout sprl_addressstyle;
	private SetPreferRelativeLayout sprl_addresspositon;
	private SetStatusRelativeLayout ssrl_intercept;
	private SetStatusRelativeLayout ssrl_applock;
	
	private final String[] styleItem = {
			"半透明","活力橙","卫士蓝","金属灰","苹果绿"
	};
	private Intent watchDogIntent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		
		ssrl_autoupdate = (SetStatusRelativeLayout) findViewById(R.id.ssrl_autoupdate);
		sprl_addressstyle = (SetPreferRelativeLayout) findViewById(R.id.sprl_showaddress_style);
		sp = getSharedPreferences("Setting", MODE_PRIVATE);

		//自动更新设置
		boolean init_autoupdate = sp.getBoolean("autoupdate", true);
		if(init_autoupdate) {
			ssrl_autoupdate.setChecked(true);
		} else {
			ssrl_autoupdate.setChecked(false);
		}
		ssrl_autoupdate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Editor editor = sp.edit();
				if(ssrl_autoupdate.isChecked()) {
					editor.putBoolean("autoupdate", false);
					ssrl_autoupdate.setChecked(false);
				} else {
					editor.putBoolean("autoupdate", true);
					ssrl_autoupdate.setChecked(true);
				}
				editor.commit();
			}
		});
		
		//归属地样式
		final int whichStyle = sp.getInt("adressstyle", 3);
		sprl_addressstyle.setDesc(styleItem[whichStyle]);
		sprl_addressstyle.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int checkedItem = sp.getInt("adressstyle", 3);
				Builder builder = new AlertDialog.Builder(SettingActivity.this);
				builder.setIcon(R.drawable.addressstyle_icon);
				builder.setTitle("请选择样式：");
				builder.setSingleChoiceItems(styleItem, checkedItem, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Editor editor = sp.edit();
						editor.putInt("adressstyle", which);
						editor.commit();
						sprl_addressstyle.setDesc(styleItem[which]);
						dialog.dismiss();
					}
				});
				builder.setNegativeButton("取消", null);
				
				builder.show();
			}
		});
		
		sprl_addresspositon = (SetPreferRelativeLayout) findViewById(R.id.sprl_showaddress_position);
		sprl_addresspositon.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(SettingActivity.this, AddressPositionActivity.class);
				startActivity(intent);
			}
		});
		
		//归属地显示是否开启 -- 在SplashActivity中判断是否开启
		ssrl_showaddress = (SetStatusRelativeLayout) findViewById(R.id.ssrl_showaddress);
		final Intent showAddrIntent = new Intent(this, ShowAddressService.class);
		
		String showAddrName = ShowAddressService.class.getName();
		boolean showaddress = sp.getBoolean("showaddress", true);
		boolean isShowAddrRunning = SystemInfoUtils.isServiceRunning(this, showAddrName);
		
		if(showaddress) {
			ssrl_showaddress.setChecked(true);
			if(!isShowAddrRunning)
				startService(showAddrIntent);
		} else {
			ssrl_showaddress.setChecked(false);
			if(isShowAddrRunning)
				stopService(showAddrIntent);
		}
		
		ssrl_showaddress.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Editor editor = sp.edit();
				if(ssrl_showaddress.isChecked()) {
					ssrl_showaddress.setChecked(false);
					editor.putBoolean("showaddress", false);
					stopService(showAddrIntent);
				} else {
					ssrl_showaddress.setChecked(true);
					editor.putBoolean("showaddress", true);
					startService(showAddrIntent);
				}
				editor.commit();
			}
		});
		
		//设置黑名单拦截 -- 在SplashActivity中判断是否开启
		ssrl_intercept = (SetStatusRelativeLayout) findViewById(R.id.ssrl_intercept);
		final Intent interceptIntent = new Intent(this, TelephonySafeService.class);
		
		String interceptName = TelephonySafeService.class.getName();
		boolean intercept = sp.getBoolean("intercept", true);
		boolean isInterceptRunning = SystemInfoUtils.isServiceRunning(this, interceptName);
		
		if(intercept) {
			ssrl_intercept.setChecked(true);
			if(!isInterceptRunning)
				startService(interceptIntent);
		} else {
			ssrl_intercept.setChecked(false);
			if(isInterceptRunning)
				stopService(interceptIntent);
		}
		
		ssrl_intercept.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Editor editor = sp.edit();
				if(ssrl_intercept.isChecked()) {
					ssrl_intercept.setChecked(false);
					editor.putBoolean("intercept", false);
					stopService(interceptIntent);
				} else {
					ssrl_intercept.setChecked(true);
					editor.putBoolean("intercept", true);
					startService(interceptIntent);
				}
				editor.commit();
			}
		});
		
		//程序锁设置 -- 在SplashActivity中判断是否开启
		ssrl_applock = (SetStatusRelativeLayout) findViewById(R.id.ssrl_applock);
		watchDogIntent = new Intent(this, WatchDogService.class);
		
		String watchDogName = WatchDogService.class.getName();
		boolean watchdog = sp.getBoolean("watchdog", false);
		boolean isWatchDogRunning = SystemInfoUtils.isServiceRunning(this, watchDogName);
		
		if(watchdog) {
			ssrl_applock.setChecked(true);
			if(!isWatchDogRunning)
				startService(watchDogIntent);
		} else {
			ssrl_applock.setChecked(false);
			if(isWatchDogRunning)
				stopService(watchDogIntent);
		}
		
		ssrl_applock.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				final Editor editor = sp.edit();
				if(ssrl_applock.isChecked()) {
					ssrl_applock.setChecked(false);
					editor.putBoolean("watchdog", false);
					stopService(watchDogIntent);
				} else {
					if(CreatePSWUtils.hasPSWSetted(SettingActivity.this)) {
						//假如设置了ES密码
						ssrl_applock.setChecked(true);
						editor.putBoolean("watchdog", true);
						startService(watchDogIntent);
					} else {
						//假如没设置ES密码
						CreatePSWUtils.showPSWDialog(SettingActivity.this, false, new PswBtn() {
							@Override
							public void onCanceledBtn() {
								Log.i(TAG, "点了取消按钮，什么都不做");
							}
							@Override
							public void onOkBtn() {
								Log.i(TAG, "点了OK按钮，设置选中状态并且保存偏好配置");
								ssrl_applock.setChecked(true);
								editor.putBoolean("watchdog", true);
								startService(watchDogIntent);
							}
						});
					}
				}
				editor.commit();
			}
		});
		
	}
	
	/**
	 * Create shortcut.
	 * @param v
	 */
	public void shortcut(View v) {
		ShortcutUtils.create(this, true);
	}
	
	/**
	 * Set or update ElephantSafe password.
	 * @param v
	 */
	public void setpsw(View v) {
		if(CreatePSWUtils.hasPSWSetted(this)) {
			CreatePSWUtils.showEnterPSWDialog(this, new PswBtn() {
				@Override
				public void onOkBtn() {
					CreatePSWUtils.showSetPSWDialog(SettingActivity.this,
							new PswBtn() {
								@Override
								public void onOkBtn() {
									Toast.makeText(SettingActivity.this, "新密码设置成功！", Toast.LENGTH_SHORT).show();
								}
								@Override
								public void onCanceledBtn() {}
							});
				}
				@Override
				public void onCanceledBtn() {}
			});
		} else
			CreatePSWUtils.showSetPSWDialog(SettingActivity.this, new PswBtn() {
				@Override
				public void onOkBtn() {
					Toast.makeText(SettingActivity.this, "密码设置成功！", Toast.LENGTH_SHORT).show();
				}
				@Override
				public void onCanceledBtn() {}
			});
	}
	
	/**
	 * Clear ElephantSafe password.
	 * @param v
	 */
	public void clearpsw(View v) {
		if(CreatePSWUtils.hasPSWSetted(this)) {
			CreatePSWUtils.showEnterPSWDialog(this, new PswBtn() {
				@Override
				public void onOkBtn() {
					Editor editor = sp.edit();
					editor.remove("password");
					ssrl_applock.setChecked(false);
					editor.putBoolean("watchdog", false);
					stopService(watchDogIntent);
					editor.commit();
					Toast.makeText(SettingActivity.this, "密码已经清除！您的手机失去了保护！", Toast.LENGTH_SHORT).show();
				}
				@Override
				public void onCanceledBtn() {}
			});
		} else
			Toast.makeText(this, "密码没有设置！", Toast.LENGTH_SHORT).show();
		
	}
}
